<template>
  <div class="white-box">
    <div class="table-mb">
      <el-button type="primary" @click="handleDetail()" :icon="CirclePlus">新增角色</el-button>
    </div>
    <div class="task-one">
      <div class="task-one__item">
        <span class="task-one__label">角色key：</span>
        <el-input placeholder="请输入角色key" v-model="searchParams.roleKey" @keyup.enter.native="handleSearch" clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">角色名称：</span>
        <el-input placeholder="请输入角色名称" v-model="searchParams.roleName" @keyup.enter.native="handleSearch" clearable></el-input>
      </div>
      <div class="task-one__btn">
        <el-button type="primary" :loading="searchBtnLoading" :icon="Search"
                   @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <el-table class="table-mb" v-loading="loading" :data="list" :header-cell-style="{ background: '#F5F7FA' }"
              element-loading-background="rgba(255, 255, 255, 0.6)">
      <el-table-column prop="roleName" label="角色名称"> </el-table-column>
      <el-table-column prop="roleKey" label="角色权限key"></el-table-column>
      <el-table-column prop="note" label="备注"></el-table-column>
      <el-table-column align="center" prop="status" label="状态" width="100">
        <template #default="{ row }">
          <el-text v-if="row.status === 0" type="success">正常</el-text>
          <el-text v-else-if="row.status === 1" type="info">停用</el-text>
          <el-text v-else type="info">-</el-text>
        </template>
      </el-table-column>
      <el-table-column prop="created" label="创建时间" width="160"></el-table-column>
      <el-table-column label="操作" width="180" fixed="right">
        <template #default="{ row }">
          <template v-if="row.level > 0">
            <el-button link type="primary" @click="handleDetail(row)">编辑</el-button>
<!--            <el-button link type="primary" @click="handleEdit(row,{sid:row.sid})">编辑</el-button>-->
            <el-popconfirm :persistent="false" title="确定删除吗？" confirm-button-text="确定" cancel-button-text="取消"
                           @confirm="handleDelete(row.sid)">
              <template #reference>
                <el-button link type="danger">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        background
        v-model:current-page="page"
        v-model:page-size="pageSize"
        :page-sizes="pageSizeNum"
        :total="totalNums"
        layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>

    <el-dialog v-model="dialogShow" :title="dialogParams.type == 'add'?'添加':'编辑'" width="660px" :append-to-body="true" :destroy-on-close="true" :close-on-click-modal="false" @close="closeDialog">
      <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules" label-width="120px" style="width: 550px">
        <el-form-item label="角色名称" prop="roleName" required>
          <el-input v-model.trim="dialogForm.roleName" clearable placeholder="请输入角色名称" :maxlength="20"/>
        </el-form-item>
        <el-form-item label="角色权限key" prop="roleKey" required>
          <el-input v-model.trim="dialogForm.roleKey" clearable placeholder="请输入角色权限key" :maxlength="20"/>
        </el-form-item>
        <el-form-item label="数据范围" prop="dataScope">
          <el-select v-model="dialogForm.dataScope" placeholder="请选择数据范围">
            <el-option :label="item.label" :value="item.value" v-for="(item, index) in selectOptions.dataScope" :key="index"/>
          </el-select>
        </el-form-item>
        <el-form-item label="角色等级" prop="level">
          <div style="width: 100%">
            <el-input-number
                v-model="dialogForm.level"
                class="mx-4"
                :min="1"
                :max="99"
                controls-position="right"
                :precision="0"
            />
            <div>
              <el-text type="danger">数值越小，等级越高；0为最高等级（超级管理员，不可添加与修改）</el-text>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="菜单权限" prop="menuSids">
          <el-cascader style="width: 100%" v-model="dialogForm.menuSids" :options="selectOptions.menuSids" :props="{multiple:true}" collapse-tags collapse-tags-tooltip clearable/>
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model.trim="dialogForm.note" clearable placeholder="请输入备注"/>
        </el-form-item>
        <el-form-item label="删除" prop="deletable">
          <el-radio-group v-model="dialogForm.deletable">
            <el-radio :value="0">可以删除</el-radio>
            <el-radio :value="1">不可以删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="dialogForm.status">
            <el-radio :value="0">正常</el-radio>
            <el-radio :value="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <div class="flexs">
            <el-button type="primary" :loading="dialogParams.loading" @click="submitDialogForm(dialogFormRef)">
              确认
            </el-button>
            <el-button @click="resetDialogForm(dialogFormRef)">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>

    <go-back/>
  </div>
</template>

<script setup>
import {
  getCurrentInstance,
  ref,
  reactive,
  onMounted,
  watch,
  computed
} from "vue"
import listMixins from "@mixins/hook/list"
import {
  useRouter
} from "vue-router"
import {
  CirclePlus, Search
} from "@element-plus/icons-vue"
import GoBack from "@components/GoBack.vue"

const router = useRouter()
const {
  proxy
} = getCurrentInstance()

/**
 * minxin列表加载
 */
const {
  page,
  pageSizeNum,
  pageSize,
  loading,
  totalNums,
  searchParams,
  searchBtnLoading,
  apiPath,
  list,
  dialogFormRef,
  dialogShow,
  dialogForm,
  dialogParams,
  handleSearch,
  getData,
  handleAdd,
  handleEdit,
  resetDialogForm,
  handleAllSelected,
  submitDialogForm,
  closeDialog,
  handleAction,
  handleDelete
} = listMixins()

const selectOptions = reactive({
  dataScope:[
    {label:'全部',value:1},
    {label:'本层组织及以下',value:2},
    {label:'本层组织',value:3},
    {label:'自定义',value:4},
  ],
  menuSids:[]
})

dialogForm.dataScope = 1;//数据范围：1=全部， 2=本层组织及以下， 3=本层组织， 4=自定义
dialogForm.deletable = 0;//是否可删除：0=可以删除，1=不可以删除，默认0
dialogForm.level = 1;//角色等级，数值越小，等级越高，0为最高等级(超级管理员，不可添加与修改)
dialogForm.menuSids	= [];//分配给该角色的菜单(权限)
dialogForm.note	= "";//备注，最多100字符
dialogForm.roleKey	= "";//角色权限key
dialogForm.roleName	= "";//角色名称
dialogForm.status	= 0;//状态：0=正常, 1=停用；默认0

apiPath.value = '/roles';

onMounted(() => {
  getData()
})
const dialogFormRules = reactive({
  roleName:[{
    required: true,
    message: "请输入角色名称",
    trigger: "blur"
  }],
  roleKey:[{
    required: true,
    message: "请输入角色权限key",
    trigger: "blur"
  }],
})
const handleDetail = (row) =>{
  let query = {};
  if(row){
    query.sid = row.sid
  }
  router.push({
    path: "/sys/roles/edit",
    query
  })
}
</script>

<style lang="less">

</style>